package com.fairytask.ui.guide
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.TweenNano;
	import com.greensock.easing.Linear;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.clip.ClipEvent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.managers.ResManager;
	import com.fairytask.engine.resource.StaticIni;
	import com.fairytask.engine.utils.IO;
	import com.jzonl.event.GuideEvent;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 剧情对白 
	 * @author hanjy
	 * 
	 */	
	public class ScenarioUI extends BaseGuide
	{
		private static var instance	:ScenarioUI;
		//顶部条
		private var _topBar			:Sprite	=	new Sprite();
		private var _top				:MovieClip;
		//底部条
		private var _bottomBar			:Sprite	=	new Sprite();
		private var _bottom			:MovieClip;
		//背景蒙版
		private var _bgMask			:Sprite	=	new Sprite();
		
		//显示文字
		private var _npcNameLab	:Label  = 	new Label();
		private var _infoText		:Text	=	new Text(730,70,"vScrol_4","vScrol_5",false,false,15);
		private var _infoSP		:Sprite = 	new Sprite();
		private var _confirmLbl	:TextField	=	new TextField();
		private var _confirmSP		:Sprite		=	new Sprite();
		private var _cancelLbl		:TextField	=	new TextField();
		private var _cancelSP		:Sprite		=	new Sprite();
		
		//半身像
		private var _clip			:Clip;
		private var _halfContainer	:Sprite;//半身像容器
		private var _shadow		:MovieClip;//半身像预加载阴影
		
		private var _txtInfoStr	:String;
		private var _timeLine		:TimelineMax;
		private var _labTimeLine	:TimelineMax;
		private var _hasPhotoNPC	:Boolean=true;//是否有npc半身像
		private var _ifAddClick	:Boolean=false;//是否需要添加单击跳过事件
		private var npcPhotoId:int;
		private var isAnimate:Boolean = true;;
		public static function getInstance():ScenarioUI
		{
			if(instance	==	null)
			{
				instance	=	new ScenarioUI();
			}
			return instance;
		}
		
		public function ScenarioUI(popupUI:Boolean=false)
		{
			super(popupUI);
		}
		
		
		/**
		 * 初始化显示 
		 */
		override public function initGUI():void
		{
			//加入蒙版
			drawMask();
			addChild(_bgMask);
			
			_top	=	ResManager.newResClass("custSkin_95") as MovieClip;		//顶
			_bottom	=	ResManager.newResClass("custSkin_95") as MovieClip;		//底
			
			//半身像一些处理==========
			_clip = new Clip(true,onLoaded);
			_clip.addEventListener(ClipEvent.RES_LOADERROR,onIOError);
			_shadow = ResManager.newResClass("SilhouetteNPC") as MovieClip;
			_halfContainer = new Sprite();
			//========================
			_top.width	=	1050;
			_top.height	=	100;
			_topBar.addChild(_top);
			addChild(_topBar);
			_bottom.width	=	1050;
			_bottom.height	=	100;
			_bottom.scaleY=-_bottom.scaleY;	//翻转之后显示在0水平线上面（即：负值）
			_bottomBar.addChild(_bottom);
			addChild(_halfContainer);
			addChild(_bottomBar);
			
			_topBar.y		=	-100;
			_bottomBar.y	=	700;
			
			_npcNameLab.x = 30;
			_npcNameLab.y = -197;
			_infoSP.addChild(_npcNameLab);
			
			_infoText.x	=	30;
			_infoText.y	=	-188;
			_infoSP.addChild(_infoText);
			addChild(_infoSP);
			
//			_confirmLbl.x		=	750;
			_confirmLbl.y		=	-130;
			_confirmSP.name	=	"confirm";
			var _tf	:TextFormat	=	new TextFormat();
			_tf.font		=	"微软雅黑";
			_tf.color		=	0xFFCB4A;
			_tf.size = 15;
			_cancelLbl.defaultTextFormat=_confirmLbl.defaultTextFormat=_tf;
			_cancelLbl.selectable = _confirmLbl.selectable = false;
			_cancelLbl.autoSize = _confirmLbl.autoSize = TextFieldAutoSize.LEFT;
			_cancelLbl.mouseEnabled = _confirmLbl.mouseEnabled = false;
			_cancelLbl.multiline = _confirmLbl.multiline = false;
			_cancelLbl.wordWrap = _confirmLbl.wordWrap = false;
			
			_confirmSP.addChild(_confirmLbl);
//			_bottomBar.addChild(_confirmSP);
			addChild(_confirmSP);
			_confirmSP.buttonMode	=	true;
			
//			_cancelLbl.x	=	850;
			_cancelLbl.y	=	-130;
			_cancelSP.name	=	"cancel";
			_cancelSP.addChild(_cancelLbl);
//			_bottomBar.addChild(_cancelSP);
			addChild(_cancelSP);
			_cancelSP.buttonMode	=	true;
			
			//事件
			_confirmSP.addEventListener(MouseEvent.CLICK,onClick);
			_cancelSP.addEventListener(MouseEvent.CLICK,onClick);
			
			_timeLine	=	new TimelineMax();
			
			_labTimeLine = new TimelineMax({repeat:-1});
			setLabFlash(_confirmLbl);
			setLabFlash(_cancelLbl);
		}
		
		/**
		 * clip加载失败 
		 * @param event
		 * 
		 */
		protected function onIOError(event:Event):void
		{
			onLoaded(null);
		}
		
		/**
		 * 绘制蒙板 
		 */
		private function drawMask():void
		{
			_bgMask.graphics.beginFill(0,0);
			_bgMask.graphics.drawRect(-220,-150,1440,900);
			_bgMask.graphics.endFill();
		}
		
		/**
		 * 场景大小改变 
		 * @param evt
		 *  
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			_topBar.width = GameStage.stage.stageWidth + 14;
			_topBar.x	= -GameStage.mainLayer.x;
			_topBar.y	= -GameStage.mainLayer.y - 100;
			
			_bottomBar.width = GameStage.stage.stageWidth + 14;
			_bottomBar.x	=	-GameStage.mainLayer.x;
			_bottomBar.y	= 	700 + GameStage.mainLayer.y;
			_halfContainer.x=	-GameStage.mainLayer.x;
			_halfContainer.y=	700 + GameStage.mainLayer.y;
			_confirmSP.x =	GameStage.mainLayer.x;
			_confirmSP.y = 700 + GameStage.mainLayer.y;
			_cancelSP.x = GameStage.mainLayer.x;
			_cancelSP.y = 700 + GameStage.mainLayer.y;
			_infoSP.x = -GameStage.mainLayer.x;
			_infoSP.y = 700 + GameStage.mainLayer.y;
		}
		
		private function setLabFlash(obj:TextField):void
		{
			_labTimeLine.insertMultiple([TweenMax.to(obj, 0.3, {glowFilter:{color:0xFF6600, alpha:1, blurX:4, blurY:4,strength:3.44}, ease:Linear.easeNone}),
							TweenMax.to(obj, 0.3, {glowFilter:Filter.COMMON_LABEL_FILTER, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			_labTimeLine.stop();
		}
		
		/**
		 *	剧情确认/取消 
		 * @param evt
		 * 
		 */		
		private function onClick(evt:MouseEvent=null):void
		{
			if(evt.target.name	==	"confirm")
			{
				onOKCancel(evt,0);
			}
			else
			{
				onOKCancel(evt,1);
			}
			_clip.clear();
			if(_halfContainer.contains(_clip))
			{
				_halfContainer.removeChild(_clip);
			}
		}
		
		override protected function onOKCancel(evt:MouseEvent=null,idx:int=0):void
		{
			_npcNameLab.htmlStr = "";
			_infoText.htmlStr 	= "";
			_confirmLbl.htmlText = "";
			_cancelLbl.htmlText 	= "";
			instance.hide();
			model.dispatchEvent(new GuideEvent(GuideEvent.SEND_CONFIRM));
			_labTimeLine.stop();
			TweenNano.to(_clip,0,{alpha:0});
		}
		
		
		/**
		 * 显示界面 
		 * 
		 */
		override public function showGuide(txt:String,choiceTxt:String="",pType:int=-1):void
		{
			if(this.hasEventListener(MouseEvent.CLICK))
			{
				this.removeEventListener(MouseEvent.CLICK,onNext);
			}
			//半透
			_bottom.alpha	=	_top.alpha	=	0.5;
			_infoText.alpha 	= 0;
			//剧情下屏蔽所有UI
			GameStage.uiSpace.visibleOther(false);
			GameStage.uiSpace.addLoad(this);	//loaderLayer
			//重新播放动画
			if(_bottom.y != -100&&isAnimate)
			{
				_timeLine.insertMultiple([TweenMax.to(_top,0.8,{y:100,alpha:.8}),TweenMax.to(_bottom,1.5,{y:-100,alpha:.8,onComplete:HalfNpc})], 0, TweenAlign.START);
				_timeLine.restart();
			}
			else
			{
				_top.alpha	=	_bottom.alpha	=	0.8;
				_top.y	=	100;
				_bottom.y	=	-100;
				HalfNpc();
			}
			super.showGuide(txt,choiceTxt);
			
			_txtInfoStr			= 	StaticIni.getSwfLangStr(txt);
			var index:int		= 	_txtInfoStr.indexOf("<br>");
			_npcNameLab.htmlStr =  	_txtInfoStr.substring(0,index);
			_infoText.htmlStr	=	_txtInfoStr.substring(index);
			var txtArr	:Array	=	choiceTxt.split(",");
			_confirmLbl.htmlText	=	StaticIni.getSwfLangStr(txtArr[0]);
			_cancelLbl.htmlText	=	"";
			//“取消”可能不存在
			if(null	!=	txtArr[1])
			{
				_cancelLbl.htmlText	=	StaticIni.getSwfLangStr(txtArr[1]);
				_cancelLbl.x	=	GameStage.rootW - 10 - _cancelLbl.width;
				_confirmLbl.x 	=	_cancelLbl.x - _cancelLbl.width - 20;
			}else
			{
				var obj:int = GameStage.rootW;
				_confirmLbl.x = GameStage.rootW - 130 - _confirmLbl.width;
				_ifAddClick=true;
			}
			_npcNameLab.alpha	= 0;
			_confirmSP.alpha 	= 0;
			_cancelLbl.alpha	= 0;
			if(_bottom.y == -100)
			{
				_infoText.alpha 	= 0;
			}
			
		}
		/**
		 * 显示半身像和文字 
		 * @param pTxt
		 * @param pPhoto
		 * @param pChoise
		 * 
		 */
		public function showWithPhoto(pTxt:String,pPhoto:int,pChoise:String="",pType:int=-1,pAnimate:Boolean=true):void
		{
			IO.traceLog("是否已经显示了===============:",isShow);
			npcPhotoId		=	pPhoto;
			isAnimate		=	pAnimate;
			showGuide(pTxt,pChoise,pType);
			if(Boolean(npcPhotoId))
			{
				TweenNano.delayedCall(1.5,ShowNpcName);
			}else
			{
				TweenNano.delayedCall(1.5,ShowTxt);
			}
		}
		
		/**
		 * 只有确定选项，此ui随便点击，跳转下一步 
		 * @param evt
		 * 
		 */		
		private function onNext(evt:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.CLICK,onNext);
			onOKCancel(null,0);
		}
		
		/**
		 * npc名称显示 
		 * 
		 */		
		private function ShowNpcName():void
		{
			TweenNano.to(_npcNameLab,0.1,{alpha:1,onComplete:ShowTxt});
		}
		
		/**
		 * 显示剧情内容 
		 * 
		 */		
		private function ShowTxt():void
		{
			TweenNano.to(_infoText,0.1,{alpha:1,onComplete:ShowLabs});
		}
		
		private function ShowLabs():void
		{
			TweenNano.to(_confirmSP,0.5,{alpha:1,onComplete:onAddClick});
			_labTimeLine.play();
		}
		
		private function onAddClick():void
		{
			if(_ifAddClick)
			{
				this.addEventListener(MouseEvent.CLICK,onNext);
			}
		}
		
		/**
		 * 场景添加NPC半身像 
		 * 
		 */		
		private function HalfNpc():void
		{
			//首行缩进2字符
			var txtFormat:TextFormat = new TextFormat();
//			txtFormat.indent = 20;
			if(Boolean(npcPhotoId))
			{
				_infoText.textFormat = txtFormat;
//				_infoText.x = 180;
				_halfContainer.y = -_shadow.height - 100;
				_halfContainer.addChild(_shadow);
				_clip.source     	= ResConfig.pngNpcPath + npcPhotoId + ".png";
				
			}else
			{
				//TweenLite.to(_clip,0,{autoAlpha:false});
				_infoText.textFormat = txtFormat;
//				_infoText.x = (1000 - _infoText.width)/2;
			}
			_infoText.alpha 	= 0;
			_confirmSP.alpha 	= 0;
		}
		
		/**
		 * 半身像图片加载完成处理 
		 * 
		 */		
		private function onLoaded(val:DisplayObject):void
		{
			if(_halfContainer.contains(_shadow))
			{
				_halfContainer.removeChild(_shadow);
			}
			_clip.y	=	-_clip.height - 177;
			_halfContainer.addChild(_clip);
			TweenNano.to(_clip,0,{alpha:0});
			TweenNano.to(_clip,1.5,{alpha:1});
			stageReSize();
		}
		
		/**
		 * 隐藏 没有动画 
		 * 
		 */
		override public function hide(e:*=null):void
		{
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			if(this.hasEventListener(MouseEvent.CLICK))
			{
				this.removeEventListener(MouseEvent.CLICK,onNext);
			}
			_top.y		=	0;
			_bottom.y	=	0;
			super.hide();
			//剧情结束，恢复屏蔽的UI
			GameStage.uiSpace.visibleOther(true);
		}
	}
}